<?php include _include(APP_PATH.'view/htm/header.inc.htm');?>

<?php
	// 公用一个模板
	if($route == 'thread' && $action == 'create') {
		$form_title = lang('thread_create');
		$form_action = url("thread-create");
		$form_submit_txt = lang('thread_create');
		$form_subject = '';
		$form_intro = '';
		$form_message = '';
		$form_doctype = 1;
		$isfirst = 1;
		$quotepid = 0;
		$info = array(
			'gsmc' => '',
			"xxdz" => "",
			'lxr' => '',
			'lxdh' => '',
			'wxh' => '',
		);
		$location = url("forum-'+jfid.checked()+'");
		$filelist = array();
	} elseif($route == 'post' && $action == 'update') {
		$form_title = lang('post_update');
		$form_action = url("post-update-$pid");
		$form_submit_txt = lang('post_update');
		$form_subject = $thread['subject'];
		$form_intro = $thread['intro'];
		$form_message = $post['message'];
		$form_doctype = $post['doctype'];
		$isfirst = $post['isfirst'];
		$quotepid = $post['quotepid'];
		$location = url("thread-$tid");

		if($isfirst == 1){
			$thread['info'] = xn_json_decode($thread['info']);
			$info = array(
				'gsmc' => $thread['info']['info_gsmc'],
				"xxdz" => $thread['info']['info_xxdz'],
				'lxr' => $thread['info']['info_lxr'],
				'lxdh' => $thread['info']['info_lxdh'],
				'wxh' => $thread['info']['info_wxh'],
			);
		}
		
	} elseif($route == 'post' && $action == 'create') {
		$form_title = lang('post_create');
		$form_action = url("post-create-$tid-0");
		$form_submit_txt = lang('post_create');
		$form_subject = '';
		$form_message = '';
		$form_doctype = 1;
		$isfirst = 0;
		//$quotepid = 0;
		$location = url("thread-$tid");
		$filelist = array();
	}
	
	// hook post_start_init.htm
	
	$filelist += (array)_SESSION('tmp_files');
?>

<!--{hook post_start.htm}-->
<style>
 .uploader__input-box {
 float: left;
 position: relative;
 margin-right: 9px;
 margin-bottom: 9px;
 width: 77px;
 height: 77px;
 border: 1px solid #d9d9d9;
 }
 .uploader__input-box:after, .uploader__input-box:before {
 content: " ";
 position: absolute;
 top: 50%;
 left: 50%;
 -webkit-transform: translate(-50%,-50%);
 transform: translate(-50%,-50%);
 background-color: #d9d9d9;
 }
 .uploader__input-box:before {
 width: 2px;
 height: 39.5px;
 }
 .uploader__input-box:after {
 width: 39.5px;
 height: 2px;
 }
 .uploader__input {
 position: absolute;
 z-index: 1;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
 opacity: 0;
 -webkit-tap-highlight-color: rgba(0,0,0,0); 
 } .text-juzhong{    float:left;	text-aline:center;	line-height:32px; }
</style>
<div class="row">
	<div class="col-lg-10 mx-auto">
		<div class="card">
			<div class="card-header">
				<?php echo $form_title; ?>
			</div>
			<div class="card-body">
				<form action="<?php echo $form_action;?>" method="post" id="form">
					<input type="hidden" name="doctype" value="<?php echo $form_doctype;?>" />
					<input type="hidden" name="quotepid" value="<?php echo $quotepid;?>" />                <div class="text-juzhong">
					<a style="color:red;">*</a><a>所在分类:</a>                </div>
					<!--{hook post_fid_before.htm}-->
					<?php if($isfirst) { ?>
					<div class="form-group">
						<!--{hook post_fid_select_before.htm}-->
						<select class="custom-select mr-1 w-auto" name="fid" id="fid">
							<?php foreach ($forumlist_allowthread as $forum) { ?>
							<option value="<?php echo $forum['fid']; ?>"><?php echo $forum['name']; ?></option>
							<?php } ?>
						</select>
						<select class="custom-select mr-1 w-auto" name="sid" id="sid">
							<?php foreach ($forum_sortlist as $_sort) { ?>
							<option value="<?php echo $_sort['sid']; ?>" <?php if($sid==$_sort['sid']){echo 'selected';} ?>><?php echo $_sort['name']; ?></option>
							<?php } ?>
						</select>
						<!--{hook post_fid_select_after.htm}-->
					</div>
					
					<?php if($route == 'thread' && $action == 'create') { ?>
					<div class="form-group d-flex flex-column">
						<div class="text-juzhong ">
						<a style="color:red;">*</a>	<a>选择服务范围(按住ctrl进行多选)
						<a href="<?php echo url('index-city-0'); ?>">(点击切换城市)</a></a>
						</div>
						<select class="custom-select" style="width: 8.5rem;" name="aid[]" id="aid" multiple>
							<?php foreach ($arealist as $_arealist){ ?>
							<option value="<?php echo $_arealist['aid']; ?>" ><?php echo $_arealist['area']; ?></option>
							<?php } ?>
						</select>
						<div class="text-juzhong ">
							<p id="">你选择的服务范围</p>
							<span id="fwfwp"></span>
						</div>
					</div>
					<?php } ?>
					<!--{hook post_subject_before.htm}-->					
					<div class="form-group d-flex flex-column">
						<div class="text-juzhong">
						<a style="color:red;">*</a>	<a>标题:</a>
						</div>
						<input type="text" class="form-control w-50"  placeholder="" name="subject" value="<?php echo $form_subject;?>" id="subject">
					</div>
					<?php if(!($route == 'post' && $action == 'create')) { ?>
					<div class="form-group d-flex flex-column">
						<div class="text-juzhong">
						<a style="color:red;">*</a>	<a>SEO关键词：<span class="text-muted">关键词长度为10-50个字建议介绍包含关键词。关键词用“,”或'|'隔开；</span></a>
						</div>
						<textarea type="text" class="form-control w-50" style="height: 100px;" placeholder="" name="intro" value="<?php echo $form_intro;?>" id="intro" ></textarea>
					</div>
					<?php } ?>
					<!--{hook post_subject_after.htm}-->
					<?php } ?>
					<style>
					.w-e-toolbar{
  						flex-wrap:wrap;
					}
					</style>
					<div class="form-group">
						<textarea class="form-control" placeholder="<?php echo lang('message');?>" name="message" id="message" style="height: 300px;display:none;"><?php echo $form_message;?></textarea>
						<div id="editor" style="">
						</div>
					</div>
					<div class="text-left">
					<a style="color:red;">*</a>	<a>添加轮播图或短视频:</a>
					</div>
					<div class="d-flex">
						<div class="uploader__input-box pl-1" id="addattach" href="javascript:void(0)">
							<input type="file"  multiple="multiple" class="uploader__input" />
						</div>
						<?php echo post_file_list_html($filelist, TRUE);?>
						<!--{hook post_bottom_right.htm}-->
					</div>
					<?php if($isfirst = 1) { ?>
						<div class="form-group">					
					<a style="color:red;">*</a>	<a>公司名称:</a><input type="text" class="form-control w-50" placeholder="" name="info_gsmc" value="<?php echo $info['gsmc'];?>" id="info_xxdz">
						</div>
					<?php } ?>
					<?php if($isfirst = 1) { ?>
						<div class="form-group">					
					<a style="color:red;">*</a>	<a>详细地址:</a><input type="text" class="form-control w-50" placeholder="" name="info_xxdz" value="<?php echo $info['xxdz'];?>" id="info_xxdz">
						</div>
					<?php } ?>

					<?php if($isfirst = 1) { ?>
						<div class="form-group">						
					<a style="color:red;">*</a>	<a>联系人:</a><input type="text" class="form-control w-25" placeholder="" name="info_lxr" value="<?php echo $info['lxr'];?>" id="info_lxr">
						</div>
					<?php } ?>

					<?php if($isfirst = 1) { ?>
			        <div class="form-group">
					<a style="color:red;">*</a>	<a>联系电话:</a><input type="text" class="form-control w-25" placeholder="" name="info_lxdh" value="<?php echo $info['lxdh'];?>" id="info_lxdh">
						</div>
					<?php } ?>

					<?php if($isfirst = 1) { ?>
						<div class="form-group">
					<a style="color:red;">*</a>	<a>微信号:</a><input type="text" class="form-control w-25" placeholder="" name="info_wxh" value="<?php echo $info['wxh'];?>" id="info_wxh">
						</div>
					<?php } ?>
					
					<!--{hook post_message_after.htm}-->

					<?php if($route == 'thread' && $action == 'create') {?>
						<div class="custom-control custom-checkbox pr-2 " >
								<input type="checkbox" class="custom-control-input" id="customCheck1">
								<label class="custom-control-label" for="customCheck1">本人已经仔细阅读并同意<span>用户服务协议、隐私政策条款。</span>您今天还可以免费发布：<?php echo $user['day_quota'] ?>条。<a style="color:red;" href="<?php echo url('my-group-up')?>">马上升级发帖套餐</a></label>
							</div>
					<?php  }?>
					<div class="d-flex justify-content-end">
							<div class="text-right mb-2">
								<button type="submit" class="btn btn-primary" id="submit" data-loading-text="<?php echo lang('submiting');?>..."> <?php echo $form_submit_txt;?> </button>
								<!--{hook post_bottom_left.htm}-->
							</div>
							<div class="ml-2">

							</div>
					</div>
					
					<!--{hook post_submit_after.htm}-->

					
					
				</form>
			</div>
		</div>
	</div>
</div>

<!--{hook post_end.htm}-->

<?php include _include(APP_PATH.'view/htm/footer.inc.htm');?>
<script src="https://unpkg.com/wangeditor@3.1.1/release/wangEditor.min.js" type="text/javascript"></script>
<script type="text/javascript">
	var E = window.wangEditor
	var editor = new E('#editor')
	var $message = $('#message')

	editor.customConfig.onchange = function(html){
		$message.val(html)
	}
	editor.customConfig.zIndex = 0
	//editor.customConfig.uploadImgShowBase64 = true
	editor.customConfig.uploadImgServer = '<?php echo url('thread-upload') ?>'
	editor.customConfig.uploadFileName = 'file'

	// editor.customConfig.menus = [
	// 	'bold',  // 粗体
	// 	'fontSize',  // 字号
	// 	'italic',  // 斜体
	// 	'underline',  // 下划线
	// 	'strikeThrough',  // 删除线
	// 	'foreColor',  // 文字颜色
	// 	'justify',  // 对齐方式
	// 	'quote',  // 引用
	// 	'image',  // 插入图片
	// 	'table',  // 表格
	// 	'undo',  // 撤销
	// ]
	editor.create()
	editor.txt.html($message.val());
</script>
<script>
var jform = $('#form');
var jsubmit = $('#submit');
var jfid = jform.find('select[name="fid"]');
jform.on('submit', function() {
	jform.reset();
	jsubmit.button('loading');
	var postdata = jform.serialize();
	$.xpost(jform.attr('action'), postdata, function(code, message) {
		if(code == 0) {
			$.alert(message);
			jsubmit.button(message).delay(1000).location('<?php echo $location;?>');
		} else if(xn.is_number(code)) {
			alert(message);
			jsubmit.button('reset');
		} else {
			$.alert(message);
			//jform.find('[name="'+code+'"]').alert(message).focus();
			jsubmit.button('reset');
		}
	});
	return false;
});

var jattachparent = $('.attachlist_parent');
$('#addattach').on('change', function(e) {
	var files = xn.get_files_from_event(e);
	if (!files) return;
	
	// 并发下会 服务端 session 写入会有问题，由客户端控制改为串行
	if (!jattachparent.find('.attachlist').length) {
		//jattachparent.append('<fieldset class="fieldset"><legend><?php echo lang('uploaded_attach');?></legend><ul class="attachlist"><ul></fieldset>');
	}
	
	var jprogress = jattachparent.find('.progress');
	if(!jprogress.length) {
		//jprogress = $('<div class="progress"><div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div></div>').appendTo(jattachparent);
	}
	jprogressbar = jprogress.find('.progress-bar');

	$.each_sync(files, function(i, callback) {
		var file = files[i];
		xn.upload_file(file, xn.url('attach-create'), {is_image: 0}, function(code, message) {
			if (code != 0) return $.alert(message);
			// 把文件 append 到附件列表
			var url = message.url;
			var filetype = message.filetype;
			var aid = message.aid;
			$('.attachlist').append('<div aid="' + aid + '" class="d-flex flex-column ml-1"><img src="' + message.url + '" target="_blank" style="width: 77px;height: 77px;"></img> <a href="javascript:void(0);" class="delete ml-3"><i class="icon-remove"></i> <?php echo lang('delete');?></a></li>');
			callback();
			jprogress.hide();
		}, function(percent) {
			percent = xn.intval(percent);
			jprogressbar.css('width', percent+'%');
			jprogressbar.text(percent+'%');
			jprogress.show();
			console.log('progress:'+ percent); 
		});
	});
});

// 删除附件
jattachparent.on('click', 'a.delete', function() {
	console.log('233');
	var jlink = $(this);
	var jli = jlink.parents('div');
	var aid = jli.attr('aid');
	//if(!window.confirm(lang.confirm_delete)) return false;
	$.xpost(xn.url('attach-delete-'+aid), function(code, message) {
		if(code != 0) return $.alert(message);
		jlink.parent().remove();
	});
	return false;
})

jform.find('[name="fid"]').checked(<?php echo $fid;?>);

$('li[data-active="fid-<?php echo $fid;?>"]').addClass('active');

</script>
<script>
	<?php 
	$arealist = area_read_all($u_city['cid']);
	arrlist_change_key($arealist,'aid'); ?>
	var arealist = <?php echo xn_json_encode($arealist); ?>;
	var areaslect = $('#aid');
	var fwfwp = $('#fwfwp');

	areaslect.on('change',function(){
		var area = areaslect.val();
		var a = [];
		area.forEach(element => {
			a += "<span class='badge badge-biaoqian'>"+arealist[element]['area']+"</span>";
		});
		fwfwp.html(a);
	})
</script>
<?php if ($route == 'thread' && $action == 'create') {?>
<script type="text/javascript">
	var list = <?php echo xn_json_encode($slist); ?>;
	var fid = $('#fid');
	var sid = $('#sid');
	fid.change(function(){
		sid.empty();
		var fids = fid.val();
		flist = list.find((e)=>{return e.fid == fids});
		console.log(flist);
		flist['sortlist'].forEach((sort)=>{
			var option = $("<option>").val(sort.sid).text(sort.name);
			sid.append(option);
		})
	})

</script>
<?php } ?>
<!--{hook post_js.htm}-->
